﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace UnityEngine.Graphics.Text
{
    public class FontStorage : IDisposable
    {
        private ContentManager _Content;

        private FontStorage()
        { }

        public static FontStorage _Instance = null;
        public static FontStorage Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new FontStorage();
                return _Instance;
            }
        }

        private Dictionary<string, SpriteFont> _Fonts = new Dictionary<string, SpriteFont>();
        public SpriteFont[] Fonts
        {
            get
            {
                return _Fonts.Values.ToArray<SpriteFont>();
            }
        }

        public void SetContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            _Content = content;
        }

        public SpriteFont LoadFont(string path)
        {
            if (_Fonts.ContainsKey(path))
                return _Fonts[path];

            SpriteFont font = _Content.Load<SpriteFont>(path);
            _Fonts.Add(path, font);
            return font;
        }

        public static void SingletonDispose()
        {
            if (_Instance != null)
                _Instance.Dispose();
            _Instance = null;
        }

        ~FontStorage()
        {
            this.Dispose(false);
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                this._Fonts.Clear();
            }
        }
    }
}
